import { User } from './types/data';
import { sqlQuery } from './modules/SQLquery'
const express = require('express');
const app = express();
const cors = require('cors');
const bodyParser = require('body-parser');

app.use(cors());

app.use(bodyParser.json());

const user: User = {
	id: 0,
	name: '',
	mail: '',
	phone: '',
};

// 登录
app.post('/dologin', (req, res) => {
	const sql = "select * from user where mail = ? and password = ?";
	const arr = [req.body.mail, req.body.password];
	sqlQuery(sql, arr, (err, data, fld) => {
		if (data && data.length) {
			//输入正确登录成功
			user.id = data[0].user_id;
			user.name = data[0].name;
			user.mail = data[0].mail
			user.phone = data[0].phone_number;
			res.send({
				code: 200,
				msg: '登录成功！',
				user: user,
			});
		} else {
			//输入账号密码错误
			res.send({ code: 500, msg: '账号密码错误' });
		}
	})
});

// 注册
app.post('/doregister', (req, res) => {
	new Promise((resolve, rejects) => {
		sqlQuery(
			"select * from user where mail = ?",
			[req.body.mail],
			(err, data) => {
				if (data.length === 0) resolve(true);
				else rejects();
			}
		);
	}).then(() => {
		const sql = "insert into user(name, mail, password, phone_number) values(?, ?, ?, ?)";
		const arr = [req.body.name, req.body.mail, req.body.password, req.body.phone_number];
		sqlQuery(sql, arr, (err, data, fld) => {
			if (!err) res.send({
				code: 200,
				message: '注册成功！',
				user_id: data.insertId,
			});
			else {
				console.log(err);
				res.send({ code: 500, message: '注册失败' });
			}
		});
	}).catch(() => {
		res.send({ code: 500, message: '邮箱已存在' });
	});
});


// 保存文章
app.post('/edit/save', (req, res) => {
	const sql = "insert into text(user_id, title, text_content, date) values(?, ?, ?, ?)";
	const arr = [req.body.user_id, req.body.title, req.body.text_content, (new Date()).toString()];
	sqlQuery(sql, arr, (err, data) => {
		if (!err) res.send({
			code: 200,
			message: '保存成功',
			text_id: data.insertId,
		})
		else {
			console.log(err);
			res.send({
				code: 500,
				message: '保存失败',
			})
		}
	});
});

// 获取所有文章
app.get('/edit/query', (req, res) => {
	const sql = "select * from text where user_id = ?";
	const arr = [req.query.user_id];
	sqlQuery(sql, arr, (err, data) => {
		if (!err) res.send({
			code: 200,
			texts: data,
		});
		else {
			console.log(err);
			res.send({
				code: 500,
				texts: data,
			})
		}
	});
});

// 更新文章
app.patch('/edit/update', (req, res) => {
	const exeQueue = [
		{
			sql: 'update text set title = ? where text_id = ?',
			arr: [req.body.title, req.body.text_id],
		},
		{
			sql: 'update text set text_content = ? where text_id = ?',
			arr: [req.body.text_content, req.body.text_id],
		}
	];
	let ok: boolean = true;
	let text_id: number = -1;
	exeQueue.map((item) => sqlQuery(item.sql, item.arr, (err, data) => {
		if (!err) ok = ok && true;
		else {
			console.log(err);
			ok = false;
		}
	}));
	if (ok) res.send({
		code: 200,
		message: '更新成功',
		text_id,
	});
	else res.send({
		code: 500,
		message: '更新失败',
	});
});

// 获取所有todoItem
app.get('/todo/query', (req, res) => {
	const sql = "select * from todoitem where user_id = ?";
	const arr = [req.query.user_id];
	sqlQuery(sql, arr, (err, data) => {
		if (data.length > 0) res.send({
			code: 200,
			msg: '获取成功！',
			items: data,
		});
		else {
			console.log(err);
			res.send({
				code: 500,
				msg: '获取失败',
				items: [],
			});
		}
	});
})

// 添加todoItem
app.post('/todo/add', (req, res) => {
	const sql = "insert into todoitem(user_id, done, item_content) values(?, ?, ?)";
	const arr = [req.body.user_id, false, req.body.name];
	sqlQuery(sql, arr, (err) => {
		if (!err) res.send({
			code: 200,
			message: '添加成功！',
		});
		else {
			console.log(err);
			res.send({
				code: 500,
				mssage: '添加失败'
			});
		}
	});
});

// 删除todoItem
app.delete('/todo/del', (req, res) => {
	const sql = "delete from todoitem where item_id = ?";
	const arr = [req.query.item_id];
	sqlQuery(sql, arr, (err) => {
		if (!err) res.send({
			code: 200,
			message: '删除成功',
		});
		else {
			console.log(err);
			res.send({
				code: 500,
				message: '删除失败',
			});
		}
	});
});

// 更新todoItem
app.patch('/todo/update', (req, res) => {
	const sql = "update todoitem set ?? = ? where item_id = ?";
	const arr = [req.body.key, req.body.value, req.body.item_id];
	sqlQuery(sql, arr, (err) => {
		if (!err) res.send({
			code: 200,
			message: '更新成功',
		});
		else {
			console.log(err);
			res.send({
				code: 500,
				message: '更新失败',
			});
		}
	});
});

app.listen(8080, () => console.log('Example app listening on port 8080!'));
